#!/bin/sh

# fail if any commands fails
set -e
# debug log
#set -x

show_usage (){
    printf "Usage: $0 [options [parameters]]\n"
    printf "\n"
    printf "Mandatory options:\n"
    printf " -g|--game          [doom|doom2|tnt|plutonia|heretic|hexen|chex3]\n"
    printf " -e|--engine        [chocolate|crispy|prboom|dsda|gzdoom]\n"
    printf " -l|--map_limit     [none|vanilla|nolimit|boom|zdoom]\n"
    printf "\n"
    printf "Options:\n"
    printf " -d|--game-dir      [/path/to/doom/base/directory] (Optional, default: '~/games/doom')\n"
    printf " -s|--skill         [1, 2, 3, 4, 5] (Optional, default: '3')\n"
    printf " -r|--map_generator [none|slige|obsidian] (Optional, default: 'none')\n"
    printf "    --levels        [number of levels to generate] (Optional, default: '1')\n"
    printf "    --rooms         [rooms to generate per level] (Optional, default: '18')\n"
    printf "    --bimo          big monsters [true|false] (Optional, default: 'true')\n"
    printf "    --bimobimo      big monsters (!) [true|false] (Optional, default: 'true')\n"
    printf "    --biwe          big weapons [true|false] (Optional, default: 'true')\n"
    printf "    --arena         generate big arena level [true|false] (Optional, default: 'false')\n"
    printf "    --minlight      [minlight value] (Optional, default: '18')\n"
    printf " -m|--mods          [none|vanilla|improved|beautiful|brutal|samsara] (Optional, default: 'vanilla')\n"
    printf " -u|--mangohud      [true|false] (Optional, default: 'no')\n"
    printf "    --nomo          no monsters [true|false] (Optional, default: 'false')\n"
    printf " -h|--help, Print help\n"

exit
}

if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
  show_usage
fi
if [ -z "$1" ]; then
  show_usage
fi

while [ ! -z "$1" ]; do
  case "$1" in
     --game-dir|-d)
        shift
        echo "INFO: param - game directory: $1"
        param_game_dir=$1
        ;;
     --game|-g)
        shift
        echo "INFO: param - iwad: $1"
        param_iwad=$1
        ;;
     --engine|-e)
        shift
        echo "INFO: param - engine: $1"
        param_engine=$1
        ;;
     --map_limit|-l)
        shift
        echo "INFO: param - map limit: $1"
        param_map_limit=$1
         ;;
     --skill|-s)
        shift
        echo "INFO: param - skill: $1"
        param_skill=$1
         ;;
     --map_generator|-r)
        shift
        echo "INFO: param - map generator: $1"
        param_map_generator=$1
         ;;
     --levels)
        shift
        echo "INFO: param - levels: $1"
        param_levels=$1
         ;;
     --rooms)
        shift
        echo "INFO: param - rooms: $1"
        param_rooms=$1
         ;;
     --bimo)
        shift
        echo "INFO: param - big monsters: $1"
        param_bimo=$1
         ;;
     --bimobimo)
        shift
        echo "INFO: param - big monsters !: $1"
        param_bimobimo=$1
         ;;
     --biwe)
        shift
        echo "INFO: param - big weapons: $1"
        param_biwe=$1
         ;;
     --arena)
        shift
        echo "INFO: param - arena level: $1"
        param_arena=$1
         ;;
     --minlight)
        shift
        echo "INFO: param - minlight: $1"
        param_minlight=$1
         ;;
     --nomo)
        shift
        echo "INFO: param - no monsters: $1"
        param_nomo=$1
         ;;
     --mods|-m)
        shift
        echo "INFO: param - mods: $1"
        param_mods=$1
         ;;
     --mangohud|-u)
        shift
        echo "INFO: param - mangohud: $1"
        param_mangohud=$1
         ;;
     *)
        show_usage
        ;;
  esac
shift
done

### Configuration
if [ -z "$param_game_dir" ]; then
      param_game_dir=~/games/doom
fi
config_script_dir="$(pwd $(dirname $0))"
config_iwads_dir=$param_game_dir/wads/iwads
if [ -z "$param_engine" ]; then
      echo "ERROR: 'engine' parameter is mandatory"
fi
if [ -z "$param_iwad" ]; then
      echo "ERROR: 'game' parameter is mandatory"
fi
if [ -z "$param_skill" ]; then
      param_skill=3
fi
if [ -z "$param_mods" ]; then
      param_mods=vanilla
fi
if [ -z "$param_nomo" ]; then
      param_nomo=false
fi
if [ -z "$param_mangohud" ]; then
      param_mangohud=false
fi
# slige config
if [ -z "$param_map_generator" ]; then
      param_map_generator=none
elif [ "$param_map_generator" = "slige" ]; then
  if [ -z "$param_levels" ]; then
        param_levels=1
  fi
  if [ -z "$param_rooms" ]; then
        param_rooms=18
  fi
  if [ -z "$param_bimo" ]; then
        param_bimo=true
  fi
  if [ -z "$param_bimobimo" ]; then
        param_bimobimo=true
  fi
  if [ -z "$param_biwe" ]; then
        param_biwe=true
  fi
  if [ -z "$param_minlight" ]; then
        #param_minlight=180
        param_minlight=100
  fi
  if [ -z "$param_arena" ]; then
        param_arena=false
  fi
fi


### check parameter values
# iwad
games="doom doom2 tnt plutonia heretic hexen chex3"
echo "$games" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_iwad" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_iwad is not a valid option, valid options are: $games"; exit 1; fi; rm match

map_limit="none vanilla nolimit boom zdoom"
echo "$map_limit" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_map_limit" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_map_limit is not a valid option, valid options are: $map_limit"; exit 1; fi; rm match

map_generators="none slige obsidian"
echo "$map_generators" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_map_generator" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_map_generator is not a valid option, valid options are: $map_generators"; exit 1; fi; rm match

mods="none vanilla improved beautiful brutal samsara"
echo "$mods" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_mods" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_mods is not a valid option, valid options are: $mods"; exit 1; fi; rm match

skills="1 2 3 4 5"
echo "$skills" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_skill" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_skill is not a valid option, valid options are: $skills"; exit 1; fi; rm match


# select map file
if [ "$param_map_generator" = "none" ]; then
  config_pwad_file=$(. "$config_script_dir"/get_doom_random_map.sh -i "$param_iwad" -l "$param_map_limit")
  config_pwad_map_number=$(. "$config_script_dir"/get_doom_mappack_random_level.sh -p "$config_pwad_file")
  echo "INFO: map number: $config_pwad_map_number"
fi

if [ "$param_map_generator" = "slige" ]; then
  . "$config_script_dir"/gen_doom_slige_map.sh -i "$param_iwad" --levels $param_levels --rooms $param_rooms --minlight $param_minlight $(if [ $param_bimo = "true" ]; then echo "--bimo true"; fi) $(if [ $param_bimobimo = "true" ]; then echo "--bimobimo true"; fi) $(if [ $param_biwe = "true" ]; then echo "--biwe true"; fi) $(if [ $param_arena = "true" ]; then echo "--arena true"; fi)
  config_pwad_file="$param_game_dir/wads/slige/slige_${param_iwad}.wad"
fi

if [ "$param_map_generator" = "obsidian" ]; then
  . "$config_script_dir"/gen_doom_obsidian_map.sh -i "$param_iwad" -l $param_map_limit
  config_pwad_file="${param_game_dir}/wads/obsidian/obsidian_${param_iwad}.wad"
fi

echo "INFO: map file: $config_pwad_file"


if [ "$param_mods" = "none" ]; then
  echo "INFO: no mods selected"
elif [ "$param_mods" = "vanilla" ]; then

fi